package com.yanqu.road.server.cmd.sacrifice;

import com.yanqu.road.logic.rank.RankListModel;
import com.yanqu.road.logic.pb.RankPb;
import com.yanqu.road.entity.enums.eSystemId;
import com.yanqu.road.entity.rank.UserRank;
import com.yanqu.road.pb.rank.RankProto;
import com.yanqu.road.pb.sacrifice.SacrificeProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.config.SacrificeConfigMgr;
import com.yanqu.road.server.manger.config.SystemOpenMgr;
import com.yanqu.road.server.manger.sacrifice.SacrificeMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.server.command.Cmd;

@Cmd(code = Protocol.S_SACRIFICE_SCENE_CHANGE, desc = "祭天场景切换")
public class SacrificeSceneChangeCmd extends AbstractCommand {

	private int type;

	private UserRank userRank;

	@Override
	public boolean parseData(YanQuMessage packet) throws Exception {
		SacrificeProto.SacrificeSceneChangeReqMsg reqMsg = SacrificeProto.SacrificeSceneChangeReqMsg.parseFrom(packet.getBytes());
		type = reqMsg.getType();
		return true;
	}

	@Override
	public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
		if(!SystemOpenMgr.systemOpen(player, eSystemId.Sacrifice.getValue())){
			ret = GameErrorCode.E_SYSTEM_NO_OPEN;
			return;
		}
		if(!SacrificeConfigMgr.isSacrificeSystemOpenTime()){
			ret = GameErrorCode.E_SYSTEM_NO_OPEN;
			return;
		}
		if(1 == type){
			SacrificeMgr.removeUserInSacrificeScene(player.getUserId());
		}else {
			if(!SacrificeConfigMgr.isInSacrificeAttackBossTime()){
				ret = GameErrorCode.E_SACRIFICE_NO_ATTACK_BOSS_TIME;
				return;
			}
			SacrificeMgr.taskRecordPlayerInfo(player);
			SacrificeMgr.addUserInSacrificeScene(player.getUserId());
			RankListModel rankListModel = SacrificeMgr.getSacrificeDamageRankList();
			if(null != rankListModel){
				userRank = rankListModel.getFirstRank();
			}
		}
	}

	@Override
	public void sendData(GamePlayer player) {
		SacrificeProto.SacrificeSceneChangeRespMsg.Builder respMsg = SacrificeProto.SacrificeSceneChangeRespMsg.newBuilder();
		respMsg.setRet(ret);
		if (0 == ret) {
			RankProto.RankTempMsg.Builder rankMsg = RankPb.parseRankTempMsg(userRank);
			if(null != rankMsg){
				respMsg.setTopOneData(rankMsg);
			}
		}
		player.sendPacket(Protocol.U_SACRIFICE_SCENE_CHANGE, respMsg);
	}

}
